﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace NetDh.Presentation.Common
{
    /*此类无用。使用MvcActionFilterAttribute代替，更灵活。*/

    public class MvcAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //如果Action设置了匿名访问，是不会进入这里，因此不可用于Tracer。
            var result = base.AuthorizeCore(httpContext);
            return result;
        }
        public override void OnAuthorization(AuthorizationContext filterContext)
        {

            //->mvc使用自定义的MvcActionFilterAttribute，而不用自带的[AllowAnonymous]，这边代码放在MvcActionFilterAttribute中。
            //IdentityHttpContext.Current.Clear();
            ////var isAuthorize = base.AuthorizeCore(httpContext);
            ////if (isAuthorize)
            //if (HttpContext.Current.User.Identity.IsAuthenticated)
            //{
            //    FormsAuthenticationTicket formTicket = ((FormsIdentity)HttpContext.Current.User.Identity).Ticket;
            //    var ticket = SecurityUtil.DecryptAES(formTicket.UserData);
            //    var token = TokenHelper.GetToken(ticket);
            //    IdentityHttpContext.Current.Token = token;
            //}
            base.OnAuthorization(filterContext);


        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            //可根据不同验证结果，返回不同的无权限结果
            base.HandleUnauthorizedRequest(filterContext);
        }
    }
}
